package com.luo.demo;

import com.luo.mapper.UserMapper;
import com.luo.pojo.User;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/registerServlet")
public class RegisterDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        SqlSession session = util00.useSqlSessionFactory().openSession();
        UserMapper mapper = session.getMapper(UserMapper.class);

//        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        User user = mapper.selectUserName(username);

        if (user != null) {
//            writer.write("该用户名已存在,请切换重试");
            writer.write("" + true);
        }
//        else {
//            Integer integer = mapper.insertUser(username, password);
//            if (integer == 1) {
//                session.commit();
//                writer.write("注册成功,请前往登录");
//                System.out.println("zccg");
//            } else {
//                System.out.println("zcsb");
//            }
//        }
        session.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
